home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98c.txt / 000039_icon-group-sender _Thu Sep 17 12:33:59 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) with SMTP id MAA18750
  4.     for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Thu, 17 Sep 1998 12:33:57 -0700 (MST)
  5. Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
  6.     id AA06376; Thu, 17 Sep 1998 12:33:28 -0700
  7. To: icon-group@optima.CS.Arizona.EDU
  8. Date: Thu, 17 Sep 1998 10:58:59 -0400
  9. From: Jerry Leichter <leichter@smarts.com>
  10. Message-Id: <360123B3.31B8@smarts.com>
  11. Organization: System Management ARTS
  12. Sender: icon-group-request@optima.CS.Arizona.EDU
  13. References: <9809161811.memo.79389@BIX.com>
  14. Subject: Re: Context Switching
  15. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  16. Status: RO
  17.  
  18. There's been a great deal of work on this general issue in the Lisp
  19. community.  In modern Lisp's there's the notion of a closure, which is a
  20. function plus its execution context as an accessible object.  A co-
  21. expression is the direct Icon analogue of a closure.  (A generator is a
  22. restricted kind of co-expression which, because it is syntactically
  23. bound to a particular point in the program, can be implemented more
  24. efficiently.)
  25.  
  26. Implementing closures efficiently is challenging.  The "obvious"
  27. implementation discards the use of a stack:  Function frames (which are
  28. closures) are allocated from the heap, and garbage-collected just like
  29. everything else.  If F call's G, then G's frame contains a return
  30. pointer to F, so as long as G is around, F can't be discarded by the GC.
  31. If the actual use of the code is just function calling, then once F
  32. returns (implying that G has returned), F's frame is now garbage (the
  33. only pointer is from G's frame, and *that* is now garbage).
  34.  
  35. In practice, this is too expensive to be practical.  So real implementa-
  36. tions play games.  For example, they might allocate frames on the stack
  37. and then copy them to the heap if one of a number of specialized calls -
  38. which can, in effect, produce a user-accessible pointer to the frame -
  39. are executed.  A cleverer example does this analysis in the compiler,
  40. and allocates the frame on the stack or heap as required right at the
  41. beginning, avoiding the copy.  Icon, in effect, uses this implementa-
  42. tion:  The only way for a closure to be "captured" is by the use of a
  43. coexpression creation, and the compiler can always recognize those.  The
  44. big difference is that Icon assumes that most things are stack-based,
  45. and produces efficient code on that assumption.  In a more aggressive
  46. closure implementation, there's a stack pointer and a current closure
  47. pointer, and code never uses the stack pointer to *find* the current
  48. closure - it always uses the closure pointer, which may point into the
  49. stack or into the heap.
  50.  
  51. There's even an interesting implementation technique - due to Henry
  52. Baker, and intended for Lisp compilers that produce C code - which uses
  53. the C stack, but in a total unconventional fashion:  Function calls are
  54. done as always, *but they never return*.  Instead, to "return", you
  55. restore the current closure (frame pointer) and PC from your caller,
  56. *but you don't pop the stack*.  Of course, then your stack gets very
  57. large after a while.  At that point, you garbage-collect it:  What's on
  58. the stack is a whole bunch of stack frames, most of which are now
  59. "dead", with no pointers to them.  You compact them out, sliding the
  60. still-live frames down, and continue.
  61.  
  62. Lisp closures are much more general and powerful than Icon co-
  63. expressions (much less generators).  What Icon got in returns was much
  64. more efficient implementations (especially for generators, which are
  65. comparable in cost to conventional function calls).  That tradeoff may
  66. no longer be quite as necessary.
  67.                             -- Jerry
  68.